﻿Imports System.Data.SqlClient
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO

Public Class frm_ConnectDatabase
    Dim dtpro As New DataProvider
    Dim tableName As New DataTable
    Dim data As New DataProvider
    Dim cnn As New SqlConnection
    Private Sub btnServerName_Click(sender As System.Object, e As System.EventArgs) Handles btnServerName.Click
        cbServerName.Items.Clear()
        Dim str As String = "Select * From sys.Servers"
        tableName = dtpro.GetTable(str)
        For i As Integer = 0 To tableName.Rows.Count - 1
            cbServerName.Items.Add(tableName.Rows(i)("name").ToString())
        Next
    End Sub

    Private Sub btnDatabase_Click(sender As System.Object, e As System.EventArgs) Handles btnDatabase.Click
        cbDbName.Items.Clear()
        Dim str As String = "DECLARE @DBuser_sql VARCHAR(4000) DECLARE @DBuser_table TABLE (DBName VARCHAR(200), UserName VARCHAR(250), LoginType VARCHAR(500), AssociatedRole VARCHAR(200)) SET @DBuser_sql='SELECT ''?'' AS DBName,a.name AS Name, a.type_desc AS LoginType,USER_NAME(b.role_principal_id) AS AssociatedRole FROM ?.sys.database_principals a LEFT OUTER JOIN ?.sys.database_role_members b ON a.principal_id=b.member_principal_id WHERE a.sid NOT IN (0x01,0x00) AND a.sid IS NOT NULL AND a.type NOT IN (''C'') AND a.is_fixed_role <> 1 AND a.name NOT LIKE ''##%'' AND ''?'' NOT IN (''master'',''msdb'',''model'',''tempdb'')ORDER BY Name'INSERT @DBuser_table EXEC sp_MSforeachdb @command1=@dbuser_sql SELECT distinct DBName FROM @DBuser_table ORDER BY DBName"
        tableName = dtpro.GetTable(str)
        For i As Integer = 0 To tableName.Rows.Count - 1
            cbDbName.Items.Add(tableName.Rows(i)("DBName").ToString())
        Next
    End Sub
    Private Sub btnSaoLuu_Click(sender As System.Object, e As System.EventArgs) Handles btnSaoLuu.Click
        If cbDbName.Text = "" Or cbServerName.Text = "" Then
            MessageBox.Show("Vui lòng chọn đầy đủ thông tin cần thiết để cấu hình hệ thống", "Thông báo")
        Else
            Dim strServerName As String = MaHoa(System.Text.Encoding.UTF8.GetBytes(cbServerName.Text))
            Dim strDbName As String = MaHoa(System.Text.Encoding.UTF8.GetBytes(cbDbName.Text))
            Dim write As StreamWriter = New StreamWriter("config.txt")
            write.WriteLine(strServerName)
            write.WriteLine(strDbName)
            write.Close()
            MessageBox.Show("Cấu hình thành công", "Thông báo")
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim encode As String
        encode = MaHoa(System.Text.Encoding.UTF8.GetBytes(TextBox1.Text))
        Dim decode As String
        decode = System.Text.Encoding.UTF8.GetString(GiaiMa(encode))
        TextBox2.Text = encode
        Label3.Text = decode
    End Sub

    Public Function MaHoa(ByVal Bydata() As Byte) As String
        If Bydata Is Nothing Then Throw New ArgumentNullException("Bydata")
        Return Convert.ToBase64String(Bydata)
    End Function

    Public Function GiaiMa(ByVal base64 As String) As Byte()
        If base64 Is Nothing Then Throw New ArgumentNullException("base64")
        Return Convert.FromBase64String(base64)
    End Function

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim strContents As String
        Dim objReader As StreamReader
        Try

            objReader = New StreamReader("config.txt")
            strContents = objReader.ReadToEnd()
            objReader.Close()
            Label3.Text = strContents.ToString()
            Label4.Text = MaHoa(System.Text.Encoding.UTF8.GetBytes(strContents))
        Catch Ex As Exception

        End Try
    End Sub
End Class